package com.cnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cnt.dao.DonationsDao;
import com.cnt.dao.RequestMoneyDao;
import com.cnt.entity.Donations;
import com.cnt.entity.RequestMoney;
import com.cnt.service.RequestMoneyService;
import com.cnt.util.FileUtil;
import com.cnt.vo.RequestMoneyVo;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class RequestMoneyServiceImpl implements RequestMoneyService {

    @Autowired
    private RequestMoneyDao requestMoneyDao;
    @Autowired
    private DonationsDao donationsDao;

    @Override
    public ResultVo<String> apply(Integer donationId, double money, String reason) {
        RequestMoney requestMoney = new RequestMoney();
        requestMoney.setReason(reason);
        requestMoney.setDonationId(donationId);
        requestMoney.setMoney(money);
        requestMoney.setTime(new Date());
        int insert = requestMoneyDao.insert(requestMoney);
        if (insert != 1) {
            return new ResultVo<>(1, "申请失败，请稍后再试", null);
        }
        Donations donation = donationsDao.getByDonationId(donationId);
        int moneyByDonationId = requestMoneyDao.getMoneyByDonationId(donationId);
        double res = donation.getDonatedAmount() - donation.getExpenseAmount() - moneyByDonationId;
        if (res < money) {
            return new ResultVo<>(2, "您可以申请的金额为:" + res, null);
        }
        return new ResultVo<>(0, "申请成功", null);
    }

    @Override
    public ResultVo<String> uploadReport(MultipartFile file, Integer fundingId) {
        if (file.getSize() > 16 * 1024 * 1024) { // 文件大小超过16MB
            return new ResultVo<>(2, "文件大于16MB，文件上传失败", "");
        }
        RequestMoney requestMoney = requestMoneyDao.selectById(fundingId);
        FileUtil fileUtil = new FileUtil();
        File file1 = null;
        try {
            file1 = fileUtil.convertMultipartFileToFile(file);
            try {
                File file2 = fileUtil.convertToPdf(file1);
                byte[] bytes = fileUtil.addWatermarkToPDF(file2, "审核专用，侵权必究");
                requestMoney.setFile(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            }
            file1.delete();
        } catch (IOException e) {
            file1.delete();
            e.printStackTrace();
        }
        if (requestMoneyDao.updateById(requestMoney) == 1) {
            return new ResultVo<>(0, "上传文件成功", "");
        }
        return new ResultVo<>(1, "上传文件失败", "");
    }

    @Override
    public InputStream downloadReport(Integer fundingId) {
        RequestMoney requestMoney = requestMoneyDao.selectById(fundingId);
        return new ByteArrayInputStream(requestMoney.getFile());
    }

    @Override
    public ResultVo<List<RequestMoney>> queryByDonationId(Integer isPerson, Integer donationId) {
        List<RequestMoney> requestMonies = null;
        if (isPerson == 0) {
            requestMonies = requestMoneyDao.queryByDonationId(donationId);
        } else {
            requestMonies = requestMoneyDao.queryPersonByDonationId(donationId);
        }
        return new ResultVo<>(0, "查询成功", requestMonies);
    }

    @Override
    public ResultVo<Double> checkMoney(Integer donationId) {
        Donations donation = donationsDao.getByDonationId(donationId);
        int moneyByDonationId = requestMoneyDao.getMoneyByDonationId(donationId);
        double res = donation.getDonatedAmount() - donation.getExpenseAmount() - moneyByDonationId;
        return new ResultVo<Double>(0, "查询成功", res);
    }

    @Override
    public ResultVo<String> agreeRequestMoney(Integer requestMoneyId, String evaluate) {

        LambdaUpdateWrapper<RequestMoney> luw = new LambdaUpdateWrapper<>();
        luw.eq(0 != requestMoneyId, RequestMoney::getRequestMoneyId, requestMoneyId)
                .ne(RequestMoney::getStatus,2)
                .set(RequestMoney::getStatus, 1)
                .set(RequestMoney::getEvaluate, evaluate);
        int i = requestMoneyDao.update(null, luw);
        if (i != 1)return new ResultVo<>(1,"已经是拨款状态，操作失败",null);
        return new ResultVo<>(0, "成功", null);
    }

    public ResultVo<String> rejectRequestMoney(Integer requestMoneyId, String evaluate) {

        LambdaUpdateWrapper<RequestMoney> luw = new LambdaUpdateWrapper<>();
        luw.eq(0 != requestMoneyId, RequestMoney::getRequestMoneyId, requestMoneyId)
                .ne(RequestMoney::getStatus,2)
                .set(RequestMoney::getStatus, 3)
                .set(RequestMoney::getEvaluate, evaluate);
        int i = requestMoneyDao.update(null, luw);
        if (i != 1)return new ResultVo<>(1,"已经是拨款状态，操作失败",null);
        return new ResultVo<>(0, "成功", null);
    }

    @Override
    public ResultVo<List<RequestMoneyVo>> getRequestMoneyVo() {
        List<RequestMoneyVo> requestMoneyVoList = requestMoneyDao.getRequestMoneyVo();
        return new ResultVo<>(0, "查询成功", requestMoneyVoList);
    }

    @Override
    public ResultVo<String> grant(Integer requestMoneyId) {
        RequestMoney requestMoney = requestMoneyDao.selectById(requestMoneyId);
        if (requestMoney.getStatus() != 1) {
            return new ResultVo<>(1, "不处于通过未拨款状态,拨款失败", null);
        }
        requestMoney.setStatus(2);
        requestMoneyDao.updateById(requestMoney);
        Integer donationId = requestMoney.getDonationId();
        LambdaUpdateWrapper<Donations> luw = new LambdaUpdateWrapper<>();
        luw.eq(Donations::getDonationId, donationId)
                .ge(Donations::getDonatedAmount, requestMoney.getMoney())
                .setSql("expense_amount = expense_amount + " + requestMoney.getMoney());
        int i = donationsDao.update(null, luw);
        if (i != 1) return new ResultVo<>(2, "已募集的资金不足，拨款失败", null);
        return new ResultVo<>(0, "成功", null);
    }

}
